<!--
 * @Author: 陈俊升 761723995@qq.com
 * @Date: 2023-12-12 11:14:35
 * @LastEditors: 陈俊升 761723995@qq.com
 * @LastEditTime: 2024-01-17 11:03:26
 * @FilePath: /hr_new_vue3/src/views/Management/Home/LeaveManagement/Certificate/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!-- 工作台-离职管理-接收任务-离职手续(办理人办理) -->
<template>
  <ConditionBox :margin-bottom="20">
    <template #left>
      <a-space>
        <div style="display: flex">
          <a-select :options="options" v-model="keyWold" />
          <a-input-search
            @press-enter="onSearch"
            @search="onSearch"
            v-model="keyWoldValue"
            placeholder="请输入"
          />
        </div>
        <a-input-search
          @press-enter="onSearch"
          @search="onSearch"
          v-model="phoneData"
          placeholder="输入手机号码搜索"
        />
        <a-button @click="reset">重置</a-button>
      </a-space>
    </template>
    <template #right>
      <a-button type="primary" @click="handleClick(null)">批量处理</a-button>
    </template>
  </ConditionBox>
  <TableTurn :tableData="getTableData" ref="tableRef" :args="args">
    <a-table-column :width="80">
      <template #title> 序号 </template>
      <template #cell="{ rowIndex }">
        {{ rowIndex + 1 }}
      </template>
    </a-table-column>
    <a-table-column :width="150" title="离职人员姓名" data-index="userName">
      <template #cell="{ record }">
        <span>{{ record.baseInfo.userName }}</span>
      </template>
    </a-table-column>
    <a-table-column :width="150" title="办理状态" data-index="handleStatus">
      <template #cell="{ record }">
        <span
          :class="{
            'text-green': handleStatus(record) === '已完成',
            'text-gray': handleStatus(record) === '待确认',
            'text-blue': handleStatus(record) === '进行中'
          }"
          >{{ handleStatus(record) }}</span
        >
      </template>
    </a-table-column>
    <a-table-column :width="150" title="操作" data-index="">
      <template #cell="{ record }">
        <!-- <a-button
              type="text"
              v-if="record.handleStatus === '已完成'"
              @click="handleClick(record, 2)"
              >查看</a-button
            > -->
        <a-button type="text" @click="handleClick(record, 3)">处理</a-button>
        <a-button type="text" @click="handleClick(record)">查看</a-button>
      </template>
    </a-table-column>
  </TableTurn>
</template>

<script lang="tsx" setup>
import { PutEntryProcedure } from '@/apis/management/human/humanChange/entry/index';
import { GetHandlerWorkHandoverList } from '@/apis/management/home/leaveManagement';
import { cloneDeep } from 'lodash';
import { Message, Modal } from '@arco-design/web-vue';

const router = useRouter();

const title = '接收任务';

const userId = +localStorage.getItem('userId');

// 模糊查询参数
const options = reactive([
  { label: '姓名', value: 'name' },
  { label: '职位', value: 'positionName' }
]);
// 模糊类型
const keyWold = ref('name');
// 模糊查询值
const keyWoldValue = ref('');

const phoneData = ref('');

let tableRef = ref();

const selectedListData = computed(() => tableRef.value.selectedListData);
const selectedList = computed(() => tableRef.value.selectedList);

// 选择器配置
const args = {
  rowSelection: {
    type: 'checkbox',
    showCheckedAll: true,
    onlyCurrent: false
  }
};

const getTableData = async (pageData?: any) => {
  try {
    let res = await GetHandlerWorkHandoverList({
      // keyWold: keyWold.value,
      // keyWoldValue: keyWoldValue.value,
      // phoneData: phoneData.value,
      ...pageData,
      id: userId
      // id: 153,
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

// 处理办理状态
const handleStatus = (record: any) => {
  if (record.baseInfo.status === 7) return '已完成';
  let totalCount = 0,
    currCount = 0;
  for (let i in record) {
    if (i !== 'baseInfo') {
      for (let el of record[i]) {
        if (el.superviseId === userId) {
          if (el.superviseHandle === 1) {
            currCount++;
          }
        }
        if (el.acceptId === userId) {
          if (el.acceptHandle === 1) {
            currCount++;
          }
        }
        if (el.superviseId === userId && el.acceptId === userId) {
          totalCount += 2;
        } else if (el.superviseId === userId || el.acceptId === userId) {
          totalCount++;
        }
      }
    }
  }
  if (currCount === 0) return '待确认';
  else if (currCount === totalCount) return '已完成';
  else return '进行中';
};

// 处理点击
const handleClick = async (record: any | null, type?: number) => {
  try {
    router.push({
      name: 'MyLeaveHandleWorkHandoverTaskDetail',
      query: {
        userId: record.baseInfo.userId,
        type: 3
      }
    });
  } catch (error: unknown) {
    console.log(error);
  }
};

const onSearch = () => {
  refresh();
};

// 重置数据
const reset = () => {
  keyWold.value = 'name';
  keyWoldValue.value = '';
  phoneData.value = '';
  refresh();
};

// 刷新列表
const refresh = (page?: any) => {
  tableRef.value.loadData(page);
};
</script>

<style lang="less" scoped></style>
